package com.qfedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.dao.CargoreceiptDao;
import com.qfedu.dao.CargoreceiptdetailDao;
import com.qfedu.dao.GoodsBillDao;
import com.qfedu.dao.GoodsbilleventDao;
import com.qfedu.entity.Cargoreceipt;
import com.qfedu.entity.Cargoreceiptdetail;
import com.qfedu.entity.Goodsbill;
import com.qfedu.entity.Goodsbillevent;
import com.qfedu.service.VehicleService;
import com.qfedu.util.Result;
import com.qfedu.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * projectName: logistics
 *
 * @author: CGY
 * time: 2020-10-9 16:11
 * description:
 */

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private CargoreceiptdetailDao cargoreceiptdetailDao;

    @Autowired
    private GoodsBillDao goodsBillDao;

    @Autowired
    private CargoreceiptDao cargoreceiptDao;

    @Autowired
    private GoodsbilleventDao goodsbilleventDao;
    @Override
    public Result selectLeftCodes() {
        List<Cargoreceiptdetail> ca = cargoreceiptdetailDao.selectAllUnSet();
        return ResultUtil.getObject(ca,"查询失败");
    }

    @Override
    public Result findGoodsBill(String goodsRevertBillId) {
        Goodsbill goodsbill = goodsBillDao.selectGoodsBillByGoodsRevertBillId(goodsRevertBillId);
        return ResultUtil.getObject(goodsbill,"查询失败");
    }

    @Override
    public Result add(Cargoreceipt cargoreceipt) {
        //查出detail表 用来获取goodbillCode
        Cargoreceiptdetail goodsDetail =
                cargoreceiptdetailDao.findByGoodsRevertBillId(cargoreceipt.getGoodsRevertBillCode());

        cargoreceipt.setBackBillState("未到车辆");
        Cargoreceipt cargoreceipt1 = cargoreceiptDao.save(cargoreceipt);

        //把货运事件表 状态改为待发
        Goodsbillevent goodsEvent =
                goodsbilleventDao.findByGoodsBillId(goodsDetail.getGoodsBillDetailId());
        goodsEvent.setEventName("待发");
        goodsbilleventDao.save(goodsEvent);
        return ResultUtil.getObject(cargoreceipt1,"添加失败");
    }

    @Override
    public PageInfo select(String backBillState, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<Cargoreceipt> list = cargoreceiptDao.findAllByBackBillState(backBillState);
        System.out.println("list = " + list);
        return new PageInfo<>(list);
    }

    @Override
    public Result selectByCode(String goodsRevertBillCode) {

        Cargoreceipt cargoreceipt = cargoreceiptDao.findByGoodsRevertBillCode(goodsRevertBillCode);
        return ResultUtil.getObject(cargoreceipt,"查询失败");
    }
}
